﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Rabbit.Logic;
using RabbitShop.Code;
using Raven.Imports.Newtonsoft.Json;

namespace RabbitShop.UI
{
    public partial class CartViewGroup : System.Web.UI.Page
    {
        protected string uid = string.Empty;
        protected List<ShopItemFull> list;
        protected string cartJson = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            string uid = string.Empty;
            if (Request.Cookies[Constant.CookieName] != null)
            {
                uid = Request.Cookies[Constant.CookieName].Values[Constant.CookieUid];
                if (string.IsNullOrEmpty(uid)) Response.Redirect("~/UI/UserLogon.aspx");
            }
            else
            {
                Response.Redirect("~/UI/UserLogon.aspx");
            }

            shopRepeater.ItemDataBound += shopRepeater_ItemDataBound;

            Customer customer = RabbitUse.GetCustomerById(Db.Store, uid);
            list = RabbitUse.GetShopItemFullList(Db.Store, customer);
            shopRepeater.DataSource = list;
            shopRepeater.DataBind();

            cartJson = JsonConvert.SerializeObject(customer.Cart);
        }

        void shopRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater repeater = e.Item.FindControl("productRepeater") as Repeater; 
                ShopItemFull data = (ShopItemFull)e.Item.DataItem; 
                repeater.DataSource = data.CartItemList;
                repeater.DataBind();
            }
        }
    }
}